package controller

import (
	"apitestAfterGo/common"
	"apitestAfterGo/domain"
	"apitestAfterGo/dto"
	"apitestAfterGo/service"
	"apitestAfterGo/utils"
	"github.com/kataras/iris/v12/mvc"
)

type SystemInfoController struct {
}

func (s *SystemInfoController) BeforeActivation(b mvc.BeforeActivation) {
	// 1 ->  HTTP 方法
	// 2 -> 路由的路径
	// 3 -> 此控制器的方法名称，该名称应为此路由的处理程序。
	b.Handle("DELETE", "/deleteSystemInfoById/{id:int}", "DeleteSystemInfoById")
	b.Handle("POST", "/getSystemInfoByPage", "GetSystemInfoByPage")
	b.Handle("GET", "/getSystemInfoById/{id:int}", "GetSystemInfoById")
	b.Handle("POST", "/addSystemInfo", "AddSystemInfo")
	b.Handle("PUT", "/modifySystemInfoById", "ModifySystemInfoById")
	b.Handle("GET", "/getSystemsBySystemId/{id:int}", "GetSystemsBySystemId")
}

func (s *SystemInfoController) DeleteSystemInfoById(id int) common.Result {
	result, _ := service.SystemInfoServiceImpl.DeleteSystemInfoById(id)
	return utils.Success(result, "删除环境信息成功！")
}

func (s *SystemInfoController) GetSystemInfoByPage(dto dto.SystemInfoPageDto) common.Result {
	count, _ := service.SystemInfoServiceImpl.GetAllSystemCount(dto)
	pages := utils.GetPages(count, dto.PageSize)
	systemInfos, _ := service.SystemInfoServiceImpl.GetSystemInfosByPage(dto)
	page := domain.Page{
		Pages:   pages,
		Total:   count,
		Current: dto.Current,
		Size:    dto.PageSize,
		Records: systemInfos,
	}
	return utils.Success(page, "获取环境信息成功！")
}

func (s *SystemInfoController) GetSystemInfoById(id int) common.Result {
	result, _ := service.SystemInfoServiceImpl.GetSystemInfoById(id)
	return utils.Success(result, "获取当前环境信息成功！")
}

func (s *SystemInfoController) AddSystemInfo(info domain.SystemInfo) common.Result {
	result, _ := service.SystemInfoServiceImpl.AddSystemInfo(info)
	return utils.Success(result, "添加环境信息成功！")
}

func (s *SystemInfoController) ModifySystemInfoById(info domain.SystemInfo) common.Result {
	result, _ := service.SystemInfoServiceImpl.ModifySystemInfoById(info)
	return utils.Success(result, "更新环境信息成功！")
}

func (s *SystemInfoController) GetSystemsBySystemId(id int) common.Result {
	systemInfos, _ := service.SystemInfoServiceImpl.GetSystemsBySystemId(id)
	return utils.Success(systemInfos, "获取环境信息成功！")
}
